package com.jzlin.funhub.admin.service.question;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzlin.funhub.admin.common.consts.CommonConst;
import com.jzlin.funhub.admin.common.consts.RedisConst;
import com.jzlin.funhub.admin.mapper.QuestionCategoryMapper;
import com.jzlin.funhub.admin.model.entity.QuestionCategory;
import com.jzlin.funhub.framework.core.cache.InvokeCache;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 问题分类表 服务实现类
 * </p>
 *
 * @author jzlin
 * @since 2025-05-14
 */
@Service
public class QuestionCategoryService extends ServiceImpl<QuestionCategoryMapper, QuestionCategory> {
    /**
     * 查询树结构
     */
    @InvokeCache(key = RedisConst.CACHE_KEY.QUESTION_CATEGORY_TREE)
    public List<Tree<Long>> queryTree() {
        List<TreeNode<Long>> allQuestionCategory = this.list().stream()
                .map(QuestionCategory::toTreeNode)
                .collect(Collectors.toList());
        return TreeUtil.build(allQuestionCategory, CommonConst.TREE_ROOT_CODE);
    }
}
